package hu.findyourguide.base.service;

import hu.findyourguide.base.domain.BaseEntity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

public class BaseServiceImpl<R extends JpaRepository<E, Long>, E extends BaseEntity> implements BaseService<E> {
	
	@Autowired
	private R repository;
	
	@Override
  public long count() {
      return repository.count();
  }
  
  @Override
  public void delete(E entity) {
		repository.delete(entity.getId());
  }
  
  @Override
  public void delete(Long id) {
		repository.delete(id);
  }
  
  @Override
  public E find(Long id) {
      return repository.findOne(id);
  }
  
  @Override
  public List<E> findAll() {
      return repository.findAll();
  }
  
  @Override
  public List<E> findEntries(int firstResult, int maxResults) {
      return repository.findAll(new org.springframework.data.domain.PageRequest(firstResult / maxResults, maxResults)).getContent();
  }
  
  @Override
  public void save(E entity) {
      repository.save(entity);
  }
  
  @Override
  public E update(E entity) {
      return repository.save(entity);
  }

}
